<?php
/**
 * 话题主页
 * 有缓存
 */
require_once dirname(dirname(__DIR__))."/common.php";

use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\model\Topic;

//实例化redis
$redis = SysResource::getRedis();
$key = REDIS_PREFIX."topic_home";
//$re_data = $redis->get($key);
$re_data="";
if(!empty($re_data)){
    $re_data=json_decode($re_data,true);
    if(!empty($re_data['data'])){
        foreach($re_data['data'] as $k=>&$v){
            if(empty($v['sub_desc'])){
                $v['sub_desc']='';
            }
        }
    }
    Response::output(0,"查询成功1",$re_data['data'],$re_data['total']);
}else{
    //实例化数据库
    $db = SysResource::getDB(false);
    $Topic = new Topic($db);

//查询推荐话题
    $topic_recommend=$Topic->topic_recommend();
    $length=count($topic_recommend['data']);
//判断话题推荐是否为空
    if($length<1){
        //如果话题推荐位为空就按时间降序取话题
        $topic_home=$Topic->topic_home("");
    }else{
        //如果话题推荐位不为空就按大于这个排序的，时间降序取
        $topic_home=$Topic->topic_home($length);
    }

    if(!empty($topic_home['data'])){
        foreach($topic_home['data'] as $k=>&$v){
        $v['topic_video_data']=$Topic->topic_video_data($v['id']);
            if(empty($v['sub_desc'])){
                $v['sub_desc']='';
            }
            if(empty($v['topic_video_data'])){
                unset($topic_home['data'][$k]);
            }
        }
    }
    $topic_home1=[];
    $topic_home1['total']=$topic_home['total'];
    foreach($topic_home['data'] as $kk=>$vv){
//        $topic_home1['data'][$kk]=$vv;
        $topic_home1['data'][]=$vv;
    }
    $redis->setex($key,10,json_encode($topic_home1));
    Response::output(0,"查询成功2",$topic_home1['data'],$topic_home1['total']);
}







